<script type="text/javascript">

$(document).ready(function () {

	$('#submit_restaurant').click(function(){
		//$("#submit_restaurant_display").empty();
		//$("#submit_restaurant_display").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
			
		var form_data = {
			name: $('#name').val(),
			address_street: $('#address_street').val(),
			address_zipcode: $('#address_zipcode').val(),
			address_city: $('#address_city').val(),
			opening_hours: $('#opening_hours').val(),
			kitchen: $('#kitchen').val(),
			fastfood: $('#fastfood').val(),
			vegetarian: $('#vegetarian').val(),
			alcohol: $('#alcohol').val(),
			takeaway: $('#takeaway').val(),
			favorite: $('#add_to_favorite').val()
		};

		
		$.ajax({
			url: "<?php echo site_url('restaurant/check_add_restaurant_form');?>",
			type: 'POST',
	        data: form_data,

	        success: function (msg) {
			var _text = "error";
			var _color = "red";
			if(msg == "error_name_exist")
			{
				_text = "The resttaurant already exists";
				_color = "orange";
			}
			else if(msg != "ok_nameok_streetok_zipcodeok_cityok_openok_kitchen")
			{
				_text = "Invalid format";
				_color = "orange";
			}
			else
			{
				_text = "Account created";
				_color = "green";
				//switch to the new page
				var _window = $('#submit_restaurant').closest("div");
				clear_window(_window);
				$(_window).append($("<div>").load("<?php echo base_url();?>application/views/insert_restaurant.php"));

			}
				$("#submit_restaurant_display").empty();
				$("#submit_restaurant_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
           }
        });
			return false;
	});
		
		$('#name').keyup(function () {
			//$("#name_display").empty();
			//$("#name_display").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
			
			var form_data = {
				name: $('#name').val()
			};
			
			$.ajax({
               url: "<?php echo site_url('restaurant/check_restaurant_name');?>",
			   type: 'POST',
               data: form_data,
               
               success: function (msg) {
					var _text = "error";
					var _color = "red";
					if(msg=="error_name"){
						_text = "Invalid characters";
						_color = "orange";
					}
					else if(msg == "error_name_exist")
					{
						_text = "Restaurant already added";
						_color = "orange";
					}
					else{
						_text = "Ok";
						_color = "green";
					}
					$("#name_display").empty();
					$("#name_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
               }
             });
			 
			return;
		});
		
		
		$('#address_street').keyup(function () {
			//$("#address_street_display").empty();
			//$("#address_street_display").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
			
			var form_data = {
				address_street: $('#address_street').val(),
			};
			
			$.ajax({
               url: "<?php echo site_url('restaurant/check_restaurant_address_street');?>",
			   type: 'POST',
               data: form_data,
               
               success: function (msg) {
					var _text = "error";
					var _color = "red";
					if(msg=="error_street"){
						_text = "Invalid length";
						_color = "orange";
					}else{
						_text = "Ok";
						_color = "green";
					}
					$("#address_street_display").empty();
					$("#address_street_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
               }
             });
			 
			return false;
		});
		
		
		$('#address_zipcode').keyup(function () {
			//$("#address_zipcode_display").empty();
			//$("#address_zipcode_display").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
			
			var form_data = {
				address_zipcode: $('#address_zipcode').val(),
			};
			
			$.ajax({
               url: "<?php echo site_url('restaurant/check_restaurant_address_zipcode/');?>",
			   type: 'POST',
               data: form_data,
               
               success: function (msg) {
					var _text = "error";
					var _color = "red";
					if(msg=="error_zipcode"){
						_text = "Invalid format";
						_color = "orange";
					}else{
						_text = "Ok";
						_color = "green";
					}
					$("#address_zipcode_display").empty();
					$("#address_zipcode_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
               }
             });
			 
			return false;
		});
		
		
		$('#address_city').keyup(function () {
			//$("#address_city_display").empty();
			//$("#address_city_display").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
			
			var form_data = {
				address_city: $('#address_city').val(),
			};
			
			$.ajax({
               url: "<?php echo site_url('restaurant/check_restaurant_address_city');?>",
			   type: 'POST',
               data: form_data,
               
               success: function (msg) {
					var _text = "error";
					var _color = "red";
					if(msg=="error_city"){
						_text = "Invalid format";
						_color = "orange";
					}else{
						_text = "Ok";
						_color = "green";
					}
					$("#address_city_display").empty();
					$("#address_city_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
               }
             });
			 
			return false;
		});
		
		
		$('#opening_hours').keyup(function () {
			//$("#opening_hours_display").empty();
			//$("#opening_hours_display").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
	
			
			var form_data = {
				opening_hours: $('#opening_hours').val(),
			};
			
			$.ajax({
               url: "<?php echo site_url('restaurant/check_restaurant_opening_hours');?>",
			   type: 'POST',
               data: form_data,
               
               success: function (msg) {
					var _text = "error";
					var _color = "red";
					if(msg=="error_open"){
						_text = "Invalid format";
						_color = "orange";
					}else{
						_text = "Ok";
						_color = "green";
					}
					$("#opening_hours_display").empty();
					$("#opening_hours_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
               }
             });
			 
			return false;
		});
		
		
		$('#kitchen').keyup(function () {
			//$("#kitchen_display").empty();
			//$("#kitchen_display").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
	
			
			var form_data = {
				kitchen: $('#kitchen').val(),
			};
			
			$.ajax({
               url: "<?php echo site_url('restaurant/check_restaurant_kitchen');?>",
			   type: 'POST',
               data: form_data,
               
               success: function (msg) {
					var _text = "error";
					var _color = "red";
					if(msg=="error_kitchen"){
						_text = "Invalid format";
						_color = "orange";
					}else{
						_text = "Ok";
						_color = "green";
					}
					$("#kitchen_display").empty();
					$("#kitchen_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
               }
             }); 
			return false;
		});


		//checkboxes
		$('#fastfood').change(function() {
		  if($('#fastfood').val() == 0)
			  $('#fastfood').val(1)
		  else
			  $('#fastfood').val(0)
		  return false;
		});
		$('#vegetarian').change(function () {
			  if($('#vegetarian').val() == 0)
				  $('#vegetarian').val(1)
			  else
				  $('#vegetarian').val(0)
		   return false;
		});

		$('#alcohol').change(function () {
			  if($('#alcohol').val() == 0)
				  $('#alcohol').val(1)
			  else
				  $('#alcohol').val(0)
		  return false;
		});
		$('#takeaway').change(function () {
			  if($('#takeaway').val() == 0)
				  $('#takeaway').val(1)
			  else
				  $('#takeaway').val(0)
		return false;
		});
		$('#add_to_favorite').change(function() {
			  if($('#add_to_favorite').val() == 0)
				  $('#add_to_favorite').val(1)
			  else
				  $('#add_to_favorite').val(0)
			  return false;
		});
	});
</script>  